﻿using System.Collections.Generic;

namespace NetService.Parser
{
    internal static class HtmlPagesParser
    {
        public static IDictionary<string, string> GetPages(string text)
        {
            var res = new Dictionary<string, string>();

            int startIndex = 0;
            KeyValuePair<string, string> link;

            do
            {
                link = GetText(text, "<h2 class=\"header\"><a property=\"dc:title\" href=\"", "\">", ref startIndex);

                if (!IsValid(link))
                    break;

                var key = link.Value.Trim();

                if (!res.ContainsKey(key))
                    res.Add(key, link.Key.Trim());

            } while (true);

            return res;
        }

        private static bool IsValid(KeyValuePair<string, string> link)
        {
            if (string.IsNullOrEmpty(link.Key))
                return false;

            return !string.IsNullOrEmpty(link.Value);
        }

        private static KeyValuePair<string, string> GetText(string text, string attrib, string endAttrib, ref int startIndex)
        {
            var url = GetUrl(text, attrib, ref startIndex, endAttrib);
            if (string.IsNullOrEmpty(url))
                return new KeyValuePair<string, string>(string.Empty, string.Empty);

            var title = GetTitle(text, "\">", "</a>", startIndex);

            if (string.IsNullOrEmpty(title))
                return new KeyValuePair<string, string>(string.Empty, string.Empty);

            return new KeyValuePair<string, string>(@"http://www.newsweek.com" + url, title);
        }

        private static string GetTitle(string text, string startAttr, string endAttr, int startIndex)
        {
            if (startIndex == -1)
                return string.Empty;

            startIndex = text.IndexOf(startAttr, startIndex);

            if (startIndex == -1)
                return string.Empty;

            startIndex += startAttr.Length;

            var endIndex = text.IndexOf(endAttr, startIndex);

            if (endIndex == -1)
                return string.Empty;

            var length = endIndex - startIndex;

            if (length <= 0)
                return string.Empty;

            return text.Substring(startIndex, length);
        }

        private static string GetUrl(string text, string attrib, ref int startIndex, string endAttrib)
        {
            startIndex = text.IndexOf(attrib, startIndex);

            if (startIndex == -1)
                return string.Empty;

            startIndex += attrib.Length;

            var endIndex = text.IndexOf(endAttrib, startIndex);

            if (endIndex == -1)
                return string.Empty;

            var length = endIndex - startIndex;

            if (length <= 0)
                return string.Empty;

            return text.Substring(startIndex, length);
        }
    }
}